/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.playground.core.entity.boardgame;

import com.playground.core.entity.Player;
import java.util.ArrayList;
import java.util.List;

/**
 * The main part of a board game.
 * A board is composition of squares and pieces, where one piece occupies a
 * single square.
 * 
 * @author Tiago Peres
 */
public class Board {
    private List<Square> squares;
    protected List<Piece> pieces;//tictactoe
    
    public Board(){
        pieces = new ArrayList<Piece>();
    }
    
    /**
     * Adds a piece to the board at the given square.
     * 
     * @param p the piece to be added
     * @param q the square where the piece will be added
     */
    public void addPieceAt(Piece p, Square q){
        p.setSquare(q);
        q.setPiece(p);
        pieces.add(p);
    }
    
    /**
     * The squares are represented by a list of squares. Getting the square at
     * position i means "listOfSquares.get(i)". If the position is valid the
     * square is returned, otherwise null is returned.
     * 
     * @param position the position representing the square in the board
     * @return the square referenced by the given position
     */
    public Square getSquareAt(int position){
        if (position >= 0 && position < squares.size()){
            return squares.get(position);
        }
        return null;
    }
    
    /**
     * Moves the specified piece to the specified square.
     * 
     * @param p the piece to be moved
     * @param s the destination square
     */
    public void movePiece(Piece p, Square s){
        p.getSquare().setPiece(null);
        p.setSquare(s);
        s.setPiece(p);
    }
    
    /**
     * Removes the specified piece from the board.
     * 
     * @param p the piece to be removed
     */
    public void removePiece(Piece p){
        p.getSquare().setPiece(null);
        pieces.remove(p);
    }
    
    /**
     * Attention: the list returned by this method is not the list of pieces
     * used by this board, it's a copy of it instead. Altering the list returned
     * by this method has no reflaction at the actual list of pieces used by
     * this board.
     * 
     * @return a list contaning all the pieces in the game
     */
    public List<Piece> getPieces(){
        return new ArrayList<Piece>(pieces);
    }
    
    /**
     * @param player the player owning the requested pieces
     * @return a list containing all the pieces in the board owned by the given player
     */
    public List<Piece> getPieces(Player player){
        ArrayList<Piece> playerPieces = new ArrayList<Piece>();
        for(Piece p : pieces){
            if (player.equals(p.getOwner())){
                playerPieces.add(p);
            }
        }
        return playerPieces;
    }
    
    protected void setSquares(List<Square> squares){
        this.squares = squares;
    }
    
    //preciso da lista no tictactoe
    protected List<Square> getSquares(){
        return this.squares;
    }
    
    /**
     * Remove all pieces in the board
     */
    public void removeAllPieces(){
        pieces = new ArrayList<Piece>();
    }
}
